package com.wjj.sixinone.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wjj.sixinone.common.enums.ExceptionEnum;
import com.wjj.sixinone.common.enums.StatusEnum;
import com.wjj.sixinone.common.exception.GlobalException;
import com.wjj.sixinone.common.pojo.dto.BedStatusDTO;
import com.wjj.sixinone.common.pojo.dto.PatientDTO;
import com.wjj.sixinone.common.pojo.entity.Patient;
import com.wjj.sixinone.common.pojo.entity.Room;
import com.wjj.sixinone.common.pojo.excel.PatientExcel;
import com.wjj.sixinone.common.pojo.vo.PatientForm;
import com.wjj.sixinone.common.pojo.vo.PatientVO;
import com.wjj.sixinone.common.util.Result;
import com.wjj.sixinone.handler.ExcelExportHandle;
import com.wjj.sixinone.mapper.BedMapper;
import com.wjj.sixinone.mapper.PatientMapper;
import com.wjj.sixinone.mapper.RoomMapper;
import com.wjj.sixinone.service.PatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小王不秃头
 * @since 2024-07-22
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private ExcelExportHandle exportHandle;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BedMapper bedMapper;
    @Override
    public Result selectPatientByLimit(PatientVO patientVO) {
        Integer limit = patientVO.getLimit();
        Integer page = patientVO.getPage();
        int index = (page - 1) * limit;
        PatientDTO patient = new PatientDTO();
        BeanUtils.copyProperties(patientVO,patient);
        patient.setIndex(index);
        List<Patient> patients= patientMapper.selectPatientByLimit(patient);
        Integer count =patientMapper.count(patient);
        return Result.ok().put("data",patients).put("count",count);
    }

    @Override
    @Transactional

    public Result addPatient(PatientForm patientForm) {
        String roomNo = patientForm.getRoomNo();
        //判断房间是否已满

        Room room = roomMapper.selectOne(new LambdaQueryWrapper<Room>().select(Room::getRoomCapacity).eq(Room::getRoomNo, roomNo));

        Integer integer = bedMapper.selectBedCount(roomNo);
        if (integer >= room.getRoomCapacity()) {
            throw new GlobalException(ExceptionEnum.BED_FULL);
        }

        //床位号冲突在床位回显部分已经处理
        BedStatusDTO bedStatusDTO = new BedStatusDTO();
        BeanUtils.copyProperties(patientForm,bedStatusDTO);
        bedStatusDTO.setBedStatus(StatusEnum.USE_NOW.getMessage());

        bedMapper.updateBedStatus(bedStatusDTO);
        //新增改变床位状态

        Patient patient = new Patient();

        BeanUtils.copyProperties(patientForm,patient);




        int insert = patientMapper.insert(patient);
        if (insert < 0) {
            throw new GlobalException(ExceptionEnum.INSERT_ERROR);
        }

        return Result.ok();
    }

    @Override
    @Transactional

    public Result updatePatientById(PatientForm patientForm) {
        Patient patient = new Patient();
        BeanUtils.copyProperties(patientForm,patient);
        // 目标床位改成使用中，原来的改成空闲
        Patient patientDb=patientMapper.selectOne(new LambdaQueryWrapper<Patient>().select(Patient::getBedNo).eq(Patient::getId,patient.getId()));
        if(patientDb==null){
            throw new GlobalException(ExceptionEnum.SELECT_ERROR);
        }
        String bedNo = patientDb.getBedNo();
        //跟换床位了

        if(!bedNo.equals(patient.getBedNo())){
            //更改的床位改变状态
            BedStatusDTO bedStatusDTO = new BedStatusDTO();
            BeanUtils.copyProperties(patientForm,bedStatusDTO);
            bedStatusDTO.setBedStatus(StatusEnum.USE_NOW.getMessage());
            bedMapper.updateBedStatus(bedStatusDTO);

            //原来的床位改变状态
            BedStatusDTO bedStatusDTO1 = new BedStatusDTO();
            BeanUtils.copyProperties(patientForm,bedStatusDTO1);
            bedStatusDTO1.setBedStatus(StatusEnum.NOT_USE.getMessage());
            bedMapper.updateBedStatus(bedStatusDTO1);
        }






        int update =patientMapper.updateById(patient);
        if (update < 0) {
            throw new GlobalException(ExceptionEnum.UPDATE_ERROR);
        }
        return Result.ok();
    }

    @Override
    public void uploadExcel(HttpServletResponse httpServletResponse) {
        List<Patient>list=new ArrayList<>();
        list=patientMapper.selectList(null);
        List<PatientExcel> excelList = new ArrayList<>();
        for (Patient patient : list) {
            PatientExcel patientExcel = new PatientExcel();
            BeanUtils.copyProperties(patient,patientExcel);
            excelList.add(patientExcel);
        }
        exportHandle.export(httpServletResponse, "病人信息", excelList,  PatientExcel.class);
    }

    @Override
    @Transactional
    public Result deleteById(Map<String, Integer> map) {
        Integer id = map.get("id");
        Patient patient = patientMapper.selectOne(new LambdaQueryWrapper<Patient>().select(Patient::getBedNo, Patient::getRoomNo).eq(Patient::getId, id));
        BedStatusDTO bedStatusDTO=new BedStatusDTO();
        BeanUtils.copyProperties(patient,bedStatusDTO);
        bedStatusDTO.setBedStatus(StatusEnum.NOT_USE.getMessage());

        bedMapper.updateBedStatus(bedStatusDTO);
        int i = patientMapper.deleteById(id);
        if (i < 0) {
            throw new GlobalException(ExceptionEnum.DELETE_ERROR);
        }
        return Result.ok();
    }
}
